<?php
/**
 * 合作网站帐号登录
 * 
 * Flan@2012-11-20
*/
class openapiAction extends action {
	
	
	/**
	 * 申请到的appid
	 */
	protected $m_appid = '';
	
	/**
	 * 申请到的appkey
	 */
	protected $m_appkey = '';
	
	/**
	 * 回调页面。
	 */
	protected $m_callback = '';
	
	/**
	 * openId
	 */
	protected $m_sOpenId = '';
	
	/**
	 * 类型  1 为 sina 2为qq
	 */
	protected $m_nOpenType = '';
	
	/**
	 * 类型数组
	 */
	protected $m_aOpenType = array('sina', 'qq');
	
	/**
	 * 类型名称  sina  qq
	 */
	protected $m_nOpenTypeName = '';
	
	/**
	 * access_token
	 */
	protected $m_sToken = '';
	
	/**
	 * 回调验证  CSRF protection
	 */
	protected $m_sCheckState = '';
    
    /**
	 * 昵称
	 */
	protected $m_sNickname = '';
    
    function __construct(){
        parent::__construct();
        //exit("---------init");
        $this->open_model = M('open:open');
        
        
	}
	
	/**
	 * 访问合作网站  window.open 打开
	 * 
	 * @access protected 
	 * @return void
	 */
	protected function loginto(){
		
	}
	
	/**
	 * 回调页面
	 *
	 * @access protected
	 * @return void
	 */
	protected function callback(){
	
	}
    public function openClose(){
        echo "<script>
              window.opener=null; 
              window.open('','_self'); 
              window.close(); 
              </script>";
    }
	
	protected function loginFlow(){
		$nMemberId = $this->getMemberId();
        //exit();
        if ($nMemberId){
			//$data = file_get_contents("http://member.che1an.com/iMemberReg/outLogin/openid/".$this->m_sOpenId."/type/".$this->m_nOpenType);
            //var_dump($data);
            //$data = json_decode($data);
            //print_r($data);
            	echo "<script> 
					window.opener.startRefresh('".$this->m_sOpenId."',".$this->m_nOpenType.");
					window.location.href = 'http://member.che1an.com/open/openqq/openClose';
			  </script>";
		} else {
		echo "<script> 
					window.opener.startRegister('".$this->m_sNickname."','".$this->m_sOpenId."',".$this->m_nOpenType.");
					window.location.href = 'http://member.che1an.com/open/openqq/openClose';
			  </script>";
              }
	}
	
	private function getMemberId(){
        //echo $this->m_sOpenId ;
        //echo $this->m_nOpenType;
        //var_dump($this->open_model);        echo 'aaa---';
       // print_r($this);
        $MemberOpen = $this->open_model->getMemberOpen($this->m_sOpenId,$this->m_nOpenType);

        //var_dump($MemberOpen);
        if($MemberOpen) 
            return $MemberOpen;
        else
            return false;
	}
	
	/**
	 * 获取cookie
	 *
	 * @access public
	 * @return array
	 */
	protected function getCookie(){
		$sCookie = cookie::get('open'.$this->m_nOpenTypeName.'');
		$aCookie = json_decode(stripcslashes($sCookie), true);
		return $aCookie;
	}
	
	/**
	 * 保存cookie
	 *
	 * @access public
	 * @param array 要保存的数据
	 * @return void
	 */
	protected function setCookie($aSave=null){
		$aCookie = $this->getCookie();
		foreach ((array) $aSave as $key => $val){
			$aCookie[$key] = $val;
		}
		cookie::set('open'.$this->m_nOpenTypeName.'', json_encode($aCookie), 3600*24*60);
	}
    /**
    *
    * @return json(array('error' => '0/1/2 0失败 1成功'，'msg'=> ''))
    */
    public function openAjax(){
        $username = sget('username');
        $password = sget('password');
        $openId = sget('openId');
        $openType = sget('openType');
        $nickname =sget('nickname');//昵称
        $type = sget('type');//1.openid关联车易安账号 2.注册
        if ($username == '' || $password == ''){
            exit(json_encode(array('error' => '0','msg'=> '账号或者密码不能为空！')));
        }                
        if($openId=='' || $openType=='' || $type==''){
            exit(json_encode(array('error' => '0','msg'=> '关联信息不完整！')));
        }
        if($type== '1'){
            $data = file_get_contents('http://member.che1an.com/iMemberReg/login/?username='.$username.'&password='.$password.'&openId='.$openId.'&openType='.$openType);  
            $data = json_decode($data);
        }elseif($type=='2'){
            //echo 'http://member.che1an.com/iMemberReg/register/?email='.$username.'&nickname='.$nickname.'&password='.$password.'&openId='.$openId.'&openType='.$openType;
            $data = file_get_contents('http://member.che1an.com/iMemberReg/register/?email='.$username.'&nickname='.$nickname.'&password='.$password.'&openId='.$openId.'&openType='.$openType);
             $data = json_decode($data);
        } 
        exit($data);
    }	
}